package top.healthylife.gateway.platform.baohu.api.util.baofoo;


import top.healthylife.gateway.platform.baohu.api.util.baofoo.rsa.RsaCodingUtil;
import top.healthylife.gateway.platform.baohu.api.util.baofoo.rsa.SecurityUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public final class FormatUtil {
    /** ==============IS Base=================== */
    /**
     * 判断是否为整数(包括负数)
     */
    public static boolean isNumber(Object arg) {
        return numberBo(0, toString(arg));
    }

    /**
     * 判断是否为小数(包括整数,包括负数)
     */
    public static boolean isDecimal(Object arg) {
        return numberBo(1, toString(arg));
    }

    /**
     * 判断是否为空 ,为空返回true
     */
    public static boolean isEmpty(Object arg) {
        return toStringTrim(arg).length() == 0 ? true : false;
    }

    /** ==============TO Base=================== */
    /**
     * Object 转换成 Int 转换失败 返回默认值 0 <br>
     * 使用:toInt(值,默认值[选填])
     */
    public static int toInt(Object... args) {
        int def = 0;
        if (args != null) {
            String str = toStringTrim(args[0]);
            // 判断小数情况。舍弃小数位
            int stri = str.indexOf('.');
            str = stri > 0 ? str.substring(0, stri) : str;
            if (args.length > 1) {
                def = Integer.parseInt(args[args.length - 1].toString());
            }
            if (isNumber(str)) {
                return Integer.parseInt(str);
            }
        }
        return def;
    }

    /**
     * Object 转换成 Long 转换失败 返回默认值 0 <br>
     * 使用:toLong(值,默认值[选填])
     */
    public static long toLong(Object... args) {
        long def = 0L;
        if (args != null) {
            String str = toStringTrim(args[0]);
            if (args.length > 1) {
                def = Long.parseLong(args[args.length - 1].toString());
            }
            if (isNumber(str)) {
                return Long.parseLong(str);
            }
        }
        return def;
    }

    /**
     * Object 转换成 Double 转换失败 返回默认值 0 <br>
     * 使用:toDouble(值,默认值[选填])
     */
    public static double toDouble(Object... args) {
        double def = 0;
        if (args != null) {
            String str = toStringTrim(args[0]);
            if (args.length > 1) {
                def = Double.parseDouble(args[args.length - 1].toString());
            }
            if (isDecimal(str)) {
                return Double.parseDouble(str);
            }
        }
        return def;
    }

    /**
     * Object 转换成 BigDecimal 转换失败 返回默认值 0 <br>
     * 使用:toDecimal(值,默认值[选填]) 特别注意: new BigDecimal(Double) 会有误差，得先转String
     */
    public static BigDecimal toDecimal(Object... args) {
        return new BigDecimal(Double.toString(toDouble(args)));
    }

    /**
     * Object 转换成 Boolean 转换失败 返回默认值 false <br>
     * 使用:toBoolean(值,默认值[选填])
     */
    public static boolean toBoolean(String bool) {
        boolean flag = isEmpty(bool) || (!"1".equals(bool) && !"true".equalsIgnoreCase(bool) && !"ok".equalsIgnoreCase(bool));
        if (flag) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Object 转换成 String 为null 返回空字符 <br>
     * 使用:toString(值,默认值[选填])
     */
    public static String toString(Object... args) {
        String def = "";
        if (args != null) {
            if (args.length > 1) {
                def = toString(args[args.length - 1]);
            }
            Object obj = args[0];
            if (obj == null) {
                return def;
            }
            return obj.toString();
        } else {
            return def;
        }
    }

    /**
     * Object 转换成 String[去除所以空格]; 为null 返回空字符 <br>
     * 使用:toStringTrim(值,默认值[选填])
     */
    public static String toStringTrim(Object... args) {
        String str = toString(args);
        return str.replaceAll("\\s*", "");
    }

    /**
     * ==============Other Base===================
     */
    public static String getNowTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    /**
     * 数字左边补0 ,obj:要补0的数， length:补0后的长度
     */
    public static String leftPad(Object obj, int length) {
        return String.format("%0" + length + "d", toInt(obj));
    }

    /**
     * 小数 转 百分数
     */
    public static String toPercent(Double str) {
        StringBuffer sb = new StringBuffer(Double.toString(str * 100.0000d));
        return sb.append("%").toString();
    }

    /**
     * 百分数 转 小数
     */
    public static Double toPercent2(String str) {
        if (str.charAt(str.length() - 1) == '%') {
            return Double.parseDouble(str.substring(0, str.length() - 1)) / 100.0000d;
        }
        return 0d;
    }

    /**
     * 将byte[] 转换成字符串
     */
    public static String byte2Hex(byte[] srcBytes) {
        StringBuilder hexRetStrBuilder = new StringBuilder();
        for (byte b : srcBytes) {
            String hexString = Integer.toHexString(0x00ff & b);
            hexRetStrBuilder.append(hexString.length() == 1 ? 0 : "").append(hexString);
        }
        return hexRetStrBuilder.toString();
    }

    /**
     * 将16进制字符串转为转换成字符串
     */
    public static byte[] hex2Bytes(String source) {
        byte[] sourceBytes = new byte[source.length() / 2];
        for (int i = 0; i < sourceBytes.length; i++) {
            sourceBytes[i] = (byte) Integer.parseInt(source.substring(i * 2, i * 2 + 2), 16);
        }
        return sourceBytes;
    }

    /**
     * String 转 Money
     */
    public static String toMoney(Object str, String moneyType) {
        DecimalFormat df = new DecimalFormat(moneyType);
        if (isDecimal(str)) {
            return df.format(toDecimal(str)).toString();
        }
        return df.format(toDecimal("0.00")).toString();
    }

    /**
     * 获取字符串str 左边len位数
     */
    public static String getLeft(Object obj, int len) {
        String str = toString(obj);
        if (len <= 0) {
            return "";
        }
        if (str.length() <= len) {
            return str;
        } else {
            return str.substring(0, len);
        }
    }

    /**
     * 获取字符串str 右边len位数
     */
    public static String getRight(Object obj, int len) {
        String str = toString(obj);
        if (len <= 0) {
            return "";
        }
        if (str.length() <= len) {
            return str;
        } else {
            return str.substring(str.length() - len, str.length());
        }
    }

    /**
     * 首字母变小写
     */
    public static String firstCharToLowerCase(String str) {
        Character firstChar = str.charAt(0);
        String tail = str.substring(1);
        str = Character.toLowerCase(firstChar) + tail;
        return str;
    }

    /**
     * 首字母变大写
     */
    public static String firstCharToUpperCase(String str) {
        Character firstChar = str.charAt(0);
        String tail = str.substring(1);
        str = Character.toUpperCase(firstChar) + tail;
        return str;
    }

    /**
     * List集合去除重复值 只能用于基本数据类型，。 对象类集合，自己写
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List delMoreList(List list) {
        Set set = new HashSet();
        List newList = new ArrayList();
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            Object element = iter.next();
            if (set.add(element)) {
                newList.add(element);
            }
        }
        return newList;
    }

    public static String formatParams(String message, Object[] params) {
        StringBuffer msg = new StringBuffer();
        String temp = "";
        for (int i = 0; i < params.length + 1; i++) {
            int j = message.indexOf("{}") + 2;
            if (j > 1) {
                temp = message.substring(0, j);
                temp = temp.replaceAll("\\{\\}", FormatUtil.toString(params[i]));
                msg.append(temp);
                message = message.substring(j);
            } else {
                msg.append(message);
                message = "";
            }
        }
        return msg.toString();
    }

    /**
     * ============== END ===================
     */
    public final static class MoneyType {
        /**
         * 保留2位有效数字，整数位每3位逗号隔开 （默认）
         */
        public static final String DECIMAL = "#,##0.00";
        /**
         * 保留2位有效数字
         */
        public static final String DECIMAL_2 = "0.00";
        /**
         * 保留4位有效数字
         */
        public static final String DECIMAL_4 = "0.0000";
    }

    private static boolean numberBo(int type, Object obj) {
        if (isEmpty(obj)) {
            return false;
        }
        int points = 0;
        int chr = 0;
        String str = toString(obj);
        for (int i = str.length(); --i >= 0; ) {
            chr = str.charAt(i);
            // 判断数字
            if (chr < 48 || chr > 57) {
                // 判断 - 号
                if (i == 0 && chr == 45) {
                    return true;
                }
                // 判断 . 号
                if (i >= 0 && chr == 46 && type == 1) {
                    ++points;
                    if (points <= 1) {
                        continue;
                    }
                }
                return false;
            }
        }
        return true;
    }


    /**
     * TreeMa集合2String
     *
     * @param data TreeMa
     * @return String
     */
    public static String coverMap2String(TreeMap<String, String> data) {
        StringBuilder sf = new StringBuilder();
        for (String key : data.keySet()) {
            if (!isBlank(data.get(key))) {
                sf.append(key).append("=").append(data.get(key).trim()).append("&");
            }
        }
        return sf.substring(0, sf.length() - 1);
    }

    /**
     * 空值判断
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (Character.isWhitespace(cs.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * 返回参数处理
     *
     * @return
     * @throws Exception
     */
    public static TreeMap<String, String> getParm(String rstr) throws Exception {
        TreeMap<String, String> dateArry = new TreeMap<String, String>();
        String[] listObj = rstr.split("&");
        for (String temp : listObj) {
            if (temp.matches("(.+?)=(.+?)")) {
                String[] tempListObj = temp.split("=");
                dateArry.put(tempListObj[0], tempListObj[1]);
            } else if (temp.matches("(.+?)=")) {
                String[] tempListObj = temp.split("=");
                dateArry.put(tempListObj[0], "");
            } else {
                throw new Exception("参数无法分解！");
            }
        }
        return dateArry;
    }

    /**
     * 获取密钥
     *
     * @param keyStr
     * @return
     * @throws Exception
     */
    public static String getAesKey(String keyStr) throws Exception {
        String[] listKeyObj = keyStr.split("\\|");
        if (listKeyObj.length == 2) {
            if (!listKeyObj[1].trim().isEmpty()) {
                return listKeyObj[1];
            } else {
                throw new Exception("Key is Null!");
            }
        } else {
            throw new Exception("Data format is incorrect!");
        }
    }

//    /*
//     * 将TreeMap 转化为key1=value1&key2=value2。。。。格式
//     * */
//    public static String formatData(TreeMap<String,String> requestBody){
//
//    	TreeSet<String> keys = new TreeSet<String>(requestBody.keySet());
//    	StringBuilder builer = new StringBuilder();
//		for(String key : keys) {
//
//			if(!requestBody.get(key).isEmpty()){
//				System.out.println(key+":"+requestBody.get(key));
//				builer.append(key).append("=").append(requestBody.get(key)).append("&");
//			}
//
//		}
//
//    	return builer.toString().substring(0, builer.toString().length() - 1);
//    }

    /*
     * 将返回的密文转换为treemap
     * */

    public static TreeMap<String, String> formatReturnData(String response) {

        TreeMap<String, String> retmap = new TreeMap<String, String>();
        String[] retdata = response.split("\\&");
        String key = null;
        String value = null;
        for (int i = 0; i < retdata.length; i++) {
            key = retdata[i].substring(0, retdata[i].indexOf("="));
            value = retdata[i].substring(retdata[i].indexOf("=") + 1, retdata[i].length());
            retmap.put(key, value);
        }
        return retmap;
    }

    /**
     * 获取加密的数字信封
     * 1.base转码
     * 2.baofu公钥加密
     */
    public static String getDgtlEnvlp(String envlp, String publicKey) throws UnsupportedEncodingException, IOException {

        String base64 = SecurityUtil.base64Encode(envlp);

        return RsaCodingUtil.encryptByPubCerFile(base64, publicKey);

    }

    /**
     * 获取解密后的数字信封
     * 1.商户私钥解密
     * 2.base解码
     */
    public static String getRetDgtlEnvlp(String envlp, InputStream pfxInputStream, String priKeyPass) throws Exception {
        String retDgtl_envlp = RsaCodingUtil.decryptByPriPfxFile(envlp, pfxInputStream, priKeyPass);

        return SecurityUtil.base64Decode(retDgtl_envlp);

    }

    /*
     * 获取AES加密的敏感信息
     * 1.base转码
     * 2.AES加密
     * */

    public static String getAesEncrypt(String info, String AesKey) throws UnsupportedEncodingException, IOException {
        String base64info = SecurityUtil.base64Encode(info);

        return SecurityUtil.aesEncrypt(base64info, AesKey);


    }

    /**
     * 获取解密的敏感信息
     * 1.base解码
     * 1.从数字信封中获取的AES解密
     */
    public static String getAesDecrypt(String encryptContent, String AesKey) throws UnsupportedEncodingException, IOException {

        String ret_info = SecurityUtil.aesDecrypt(encryptContent, AesKey);

        return SecurityUtil.base64Decode(ret_info);


    }

    //n表示随机生成字符串的长度

    public static String getRandom(int length) {
        String all = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        char[] chars = all.toCharArray();

        String res = "";
        for (int i = 0; i < length; i++) {
            int id = (int) (Math.random() * (all.length() - 1));
            res = res + chars[id];
        }
        return res;
    }
}
